package com.bpf.base.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bpf.base.base.vo.TreeNode;
import com.bpf.base.cache.AppService;
import com.bpf.base.model.system.Dict;
import com.bpf.base.util.Collections3;

@Controller
@RequestMapping("/format")
public class FormatController extends BaseController {
	
	@Autowired
	private AppService appService;

	@ResponseBody
	@RequestMapping("/get")
	public Dict get(@RequestParam String type,@RequestParam String code){
		List<Dict> list = appService.asMap().get(type);
		for(Dict d :list){
			if(d.getCode().equals(code))
				return d;
		}
		return null;
	}
	
	@ResponseBody
	@RequestMapping("/list")
	public List<Dict> list(@RequestParam String type){
		return appService.asMap().get(type);
	}
	
	@ResponseBody
	@RequestMapping("/combobox/{type}")
	public List<Dict> combobx(@PathVariable(value="type") String type){
		return appService.asMap().get(type);
	}
	
	@RequestMapping("/tree")
	@ResponseBody
	public List<TreeNode> tree(String type){
		List<TreeNode> tree=new ArrayList<TreeNode>();
		List<Dict> list = appService.asMap().get(type);
		if(!Collections3.isEmpty(list)){
			for(Dict dict :list){
				TreeNode Tin=new TreeNode();
				Tin.setId(dict.getCode());
				Tin.setText(dict.getValue());
				Tin.setParentId("PLATFORM_TYPE");
				tree.add(Tin);
			}
		}
		return tree;
	}
}
